package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyMessageVerifyCode;
import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyMobilePhoneNumber;
import static com.chain.BrotherExchange.viewmodel.VerificationManage.verifyPassword;

public class RegisterViewModel extends AndroidViewModel {

    private int region;
    private String phone;

    private MutableLiveData<Resource<String>> mSendMsmLiveData;
    private MutableLiveData<Resource<String>> mRegisterLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mVerificationResult;
    private MutableLiveData<Resource<JSONObject>> mStartVerificationLiveData;

    public RegisterViewModel(@NonNull Application application) {
        super(application);
        mSendMsmLiveData = new MutableLiveData<>();
        mRegisterLiveData = new MutableLiveData<>();
        mVerificationResult = new MutableLiveData<>();
        mStartVerificationLiveData = new MutableLiveData<>();
    }

    public MutableLiveData<Resource<String>> getSendMsmCall() {
        return mSendMsmLiveData;
    }

    public MutableLiveData<Resource<JSONObject>> startVerification(String phone, int region) {
        if (VerificationManage.verifyMobilePhoneNumber(phone)) {
            this.phone = phone;
            this.region = region;
            DataRepository.get().gettestApiFirst(phone, result -> {
                if (result.isSucceed()) {
                    try {
                        mStartVerificationLiveData.setValue(Resource.success(
                                new JSONObject(new Gson().toJson(result.getData()))));
                    } catch (JSONException e) {
                        mStartVerificationLiveData.setValue(Resource.error(e.getMessage()));
                    }
                } else {
                    mStartVerificationLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        }

        return mStartVerificationLiveData;
    }

    public MutableLiveData<Resource<BaseResponseModel>> submitVerificationResult(
            String challenge, String validate, String code) {
        DataRepository.get().gettestApiSecond(phone, challenge, validate, code, 1, result -> {
            if (result.isSucceed()) {
                mVerificationResult.setValue(Resource.success(result.getMsg()));
            } else {
                mVerificationResult.setValue(Resource.error(result.getMsg()));
            }
        });

        return mVerificationResult;
    }

    public MutableLiveData<Resource<String>> sendMessage() {
            mSendMsmLiveData.setValue(Resource.loading());
            String regionStr = String.valueOf(region);
            DataRepository.get().sendMessage(regionStr, phone, C.API_REGISTER_TYPE, result -> {
                if (result.isSucceed()) {
                    mSendMsmLiveData.setValue(Resource.success(result.getMsg()));
                } else {
                    mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        return mSendMsmLiveData;
    }

    public MutableLiveData<Resource<String>> register(
            String mobilePhone, String password, int region, String inviteCode, String code) {
        if (verifyMobilePhoneNumber(mobilePhone) && verifyMessageVerifyCode(code)
                && verifyPassword(password)) {
            mRegisterLiveData.setValue(Resource.loading());
            DataRepository.get().register(mobilePhone, password, region, inviteCode, code, result -> {
                if (result.isSucceed()) {
                    mRegisterLiveData.setValue(Resource.success(result.getMsg()));
                } else {
                    mRegisterLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        }

        return mRegisterLiveData;
    }
}
